// pages/shiwu/shiwu.js
var choose=[]
let searchKey= ''
Page({
  data: {
    date:'2022-09-20',
    flag: false,
    //顶部按钮
    topNum : 0,
  ////////
    items: [
    ],
  selectList: [
    {
    name: '全部状态',
    list: [
      { id: 1, content: '待解决' },
      { id: 2, content: '已解决' },
      { id: 3, content: '全部状态' }
    ],
  },
  {
      name: '所有种类',
      list: [
        { id: 1, content: '所有种类' },
        { id: 2, content: '身份证' },
        { id: 3, content: '学生证' },
        { id: 4, content: '饭卡' },
        { id: 5, content: '雨伞' },
        { id: 6, content: '书籍' },
        { id: 7, content: '水杯' },
        { id: 8, content: '衣服' },
        { id: 9, content: '其他' }
      ]
    }
  ],
  showIndex: -1,
  ///////
  },
  //各种方法区
  onShow(){
   this.getItems()
  },
  getItems(){
    wx.cloud.database().collection('lostItem').where({
      typeId: 1,
      date:this.date
    }).get({
      success: res => {
        console.log('item',res.data)
        this.setData({
          items: res.data
        })
      }
    })
  },
  //回到顶部按钮
  toTop() { // 回到顶部
    this.setData({
        topNum:0
    })
},

  //物品详细介绍页面
  toItemInfo(e){
    let itemId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/iteminfo/iteminfo?itemId='+itemId,
    })
  },
  //搜索功能区
  //获取用户输入内容
  getSearch(e){
   console.log(e.detail.value)
   searchKey=e.detail.value
  },
//触发搜索事件
  goSearch(){
    console.log('触发了搜索',searchKey)
     if(searchKey&&searchKey.length>0){
       //触发搜索写在这里
       wx.cloud.database().collection('lostItem').where({
         typeId:1,
        title: wx.cloud.database().RegExp({
          regexp: '.*' + searchKey + '.*',     //根据keyWord模糊查询
          options: 'i'                       //不区分大小写
        })
      }).get({
        success:res=>{
        this.setData({
          items:res.data
        })
        }
      })

     }else{
       wx.showToast({
         icon:'none',
         title: '搜索词为空',
       })
     }
 
  },
  bindDateChange(e){
    let date = e.detail.value
    console.log('选择的日期',date)
    choose[2]=date
    this.getChooseItem(choose[0],choose[1],choose[2])
    this.setData({
      date: date,
      flag: true
    })
  },
chooseTab(e){
  let index = e.currentTarget.dataset.index;
  if(index !== this.data.showIndex){
    this.setData({
      showIndex: index
    })
  }else{
    // 再次点击应该收起
    this.setData({
      showIndex: -1
    })
  }
},
// 筛选数据
chooseOption(e){
  let val = e.currentTarget.dataset.value,
    idx = e.currentTarget.dataset.index;
    console.log('选择了',val,idx)
    if(val==='所有种类' || val ==='全部状态'){
      val = null
    }
    choose[idx]= val
  this.setData({
    [`selectList[${idx}].active`]: val,
    showIndex: -1
  })
  //根据不同条件限制筛选
  this.getChooseItem(choose[0],choose[1],choose[2])
},
 //根据条件获取数据
 getChooseItem(choose1,choose2,choose3){
   console.log('选择条件',choose)
   //三个条件限制
   if(choose1!==null && choose2!==null &&   choose3!==null){
     wx.cloud.database().collection('lostItem').where({
       status: choose1,
       categoryName: choose2,
       date: choose3,
       typeId:1
     }).get({
       success: res => {
         this.setData({
           items: res.data
         })
       }
     })
     return
   }
   //选择两个条件
   else if((choose1===null) && choose2!==null && choose3!==null){
    wx.cloud.database().collection('lostItem').where({
      categoryName: choose2,
      date: choose3,
      typeId:1
    }).get({
      success: res => {
        this.setData({
          items: res.data
        })
      }
    })
  }
  else if(choose1!==null && (choose2===null) && choose3!==null){
    wx.cloud.database().collection('lostItem').where({
      status: choose1,
      date: choose3,
      typeId:1
    }).get({
      success: res => {
        this.setData({
          items: res.data
        })
      }
    })
  }
  else if(choose1!==null && choose2!==null  && choose3===null){
    wx.cloud.database().collection('lostItem').where({
      status: choose1,
      categoryName: choose2,
      typeId:1
    }).get({
      success: res => {
        this.setData({
          items: res.data
        })
      }
    })
  }
  else if(choose1!==null && (choose2===null) && choose3===null){
    wx.cloud.database().collection('lostItem').where({
      status: choose1,
      typeId:1
    }).get({
      success: res => {
        this.setData({
          items: res.data
        })
      }
    })
  }
  //只选择了日期
 else if((choose1===null) && (choose2===null) && choose3!==null){
    wx.cloud.database().collection('lostItem').where({
      date: choose3,
      typeId:1
    }).get({
      success: res => {
        this.setData({
          items: res.data
        })
      }
    })
  }
 else if((choose1===null) && choose2!==null && choose3===null){
    wx.cloud.database().collection('lostItem').where({
      categoryName: choose2,
      typeId:1
    }).get({
      success: res => {
        this.setData({
          items: res.data
        })
      }
    })
  }
  else if(choose1===null && choose2===null && choose3===null){
    wx.cloud.database().collection('lostItem').where({
      typeId:1
    }).get({
      success: res => {
        this.setData({
          items: res.data
        })
      }
    })
  }
  else{
    this.getItems()
  }
 },

////
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})